//
//  MetalsView.m
//  Project two framework
//
//  Created by 张峥 on 16/7/25.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "MetalsView.h"
#import "FundCollectionViewCell.h"
#import "ZZConfig.h"
#import "MetalCollectionViewCell.h"
#import "ZPZNetRequest+MetalsOtherInfo.h"
#import "UIImageView+WebCache.h"
#import "PopTView.h"
#import "MJRefresh.h"

#define PHOTO_URL @"http://nmzh.rimiedu.com"
static NSInteger  indexA;
static NSInteger  indexB;
static NSInteger  indexC;
static NSInteger  indexD;

@interface MetalsView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIScrollView          *scrollView;
@property (nonatomic, strong) UICollectionView      *collectView;
@property (nonatomic, assign) NSInteger             index;
@property (nonatomic, strong) NSMutableArray        *dataSource;
@property (nonatomic, strong) NSMutableArray        *incomeArr;
@property (nonatomic, strong) NSMutableArray        *classArr;
@property (nonatomic, strong) NSMutableArray        *suppliersArr;
@property (nonatomic, strong) NSMutableArray        *peopleArr;
@property (nonatomic, strong) NSString              *typeStr;
@property (nonatomic, strong) NSString              *ageStr;
@property (nonatomic, strong) NSString              *purposeStr;
@property (nonatomic, strong) NSString              *supplierStr;
@property (nonatomic, strong) UILabel               *myLabel;

@end

@implementation MetalsView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self stepUI];
    }
    return self;
}

- (void)loadDataSource{
    _incomeArr = [NSMutableArray array];
    _classArr = [NSMutableArray array];
    _suppliersArr = [NSMutableArray array];
    _peopleArr = [NSMutableArray array];
    _ageStr = @"";
    _supplierStr = @"";
    _purposeStr = @"";
    _typeStr = @"";
    [ZPZNetRequest getMealsListtPageNum:@"1" pageSize:@"10" typeId:@"" ageId:@"" purposeId:@"" supplierId:@"" userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
            [_collectView reloadData];
        }
    }];
    //年龄
    [ZPZNetRequest getMetalsOtherInfoComletion:^(id object, BOOL success) {
        if (success) {
            if (success) {
                NSArray *array = object[@"content"][@"ages"];
                NSArray *array1 = object[@"content"][@"purposes"];
                NSArray *array2 = object[@"content"][@"suppliers"];
                NSArray *array3 = object[@"content"][@"types"];
                [_incomeArr addObject:@"默认"];
                for (NSDictionary *dic in array) {
                [_incomeArr addObject:dic[@"name"]];
                }
                [_classArr addObject:@"默认"];
                for (NSDictionary *dic in array3) {
                    [_classArr addObject:dic[@"name"]];
                }
                [_suppliersArr addObject:@"默认"];
                for (NSDictionary *dic in array2) {
                    [_suppliersArr addObject:dic[@"name"]];
                }
                [_peopleArr addObject:@"默认"];
                for (NSDictionary *dic in array1) {
                    [_peopleArr addObject:dic[@"name"]];
                }
            }
        }
    }];
}


- (void)stepUI{
    [self loadDataSource];
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.collectView];
    NSArray *array = @[@"类别:",@"供应商:",@"适宜人群(按年龄):",@"适宜人群(按意向):"];
    _index = 140;
    //循环创建label
    for (NSInteger i = 0; i < 4; i++) {
        UILabel *label = [[UILabel alloc] init];
        if (i == 3) {
            label.frame = CGRectMake(10 + 200*i, 0, 70, 40);
        }else{
            label.frame = CGRectMake(10 + 210*i, 0, 70, 40);
        }
        label.numberOfLines = 2;
        label.textColor = WHITE_C;
        label.font = [UIFont systemFontOfSize:15 weight:2];
        label.text = array[i];
        [self addSubview:label];
    }
    //循环创建button
    for (NSInteger i = 0 ; i < 4; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitle:@"默认" forState:UIControlStateNormal];
        button.backgroundColor = WHITE_C;
        if (i == 0) {
            button.frame = CGRectMake(60 + 210*i, 0, 150, 40);
            [button addTarget:self action:@selector(classBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = 200;
        }if (i == 1) {
            button.frame = CGRectMake(70 + 210*i, 0, 140, 40);
            [button addTarget:self action:@selector(superBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = 201;
        }
        if( i == 2){
            button.frame = CGRectMake(80 + 210*i, 0, 100, 40);
            [button addTarget:self action:@selector(peopleBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = 202;
        }if (i == 3) {
            button.frame = CGRectMake(60 + 210*i, 0, 150, 40);
            [button addTarget:self action:@selector(typeBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = 203;
        }
        [self addSubview:button];
    }
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataTableView:) name:@"ghy" object:nil];
}

#pragma mark   ---  自定义方法
- (void)refreshHeader{
    [ZPZNetRequest getMealsListtPageNum:@"1" pageSize:@"10" typeId:@"" ageId:@"" purposeId:@"" supplierId:@"" userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
            [_collectView reloadData];
            [_scrollView.mj_header endRefreshing];
        }
    }];
}


#pragma mark   ---  通知
- (void)reloadDataTableView:(NSNotification *)sender{
    indexA = 0;
    indexB = 0;
    indexC = 0;
    indexD = 0;
    NSArray *arr = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
    if ([@"class" isEqualToString:sender.userInfo[@"key"]]) {
        UIButton *button = [self viewWithTag:200];
        [button setTitle:sender.userInfo[@"name"] forState:UIControlStateNormal];
        for (NSString *string in _classArr) {
            if ([string isEqualToString:sender.userInfo[@"name"]]) {
                _typeStr = arr[indexA];
            }
            indexA ++;
        }
    }
    if ([@"suppliers" isEqualToString:sender.userInfo[@"key"]]) {
        UIButton *button = [self viewWithTag:201];
        [button setTitle:sender.userInfo[@"name"] forState:UIControlStateNormal];
        for (NSString *string in _suppliersArr) {
            if ([string isEqualToString:sender.userInfo[@"name"]]) {
                _supplierStr = arr[indexB];
            }
            indexB ++;
        }
    }
    if ([@"income" isEqualToString:sender.userInfo[@"key"]]) {
        UIButton *button = [self viewWithTag:202];
        [button setTitle:sender.userInfo[@"name"] forState:UIControlStateNormal];
        for (NSString *string in _incomeArr) {
            if ([string isEqualToString:sender.userInfo[@"name"]]) {
                _ageStr = arr[indexC];
            }
            indexC ++;
        }
    }
    if ([@"people" isEqualToString:sender.userInfo[@"key"]]) {
        UIButton *button = [self viewWithTag:203];
        [button setTitle:sender.userInfo[@"name"] forState:UIControlStateNormal];
        for (NSString *string in _peopleArr) {
            if ([string isEqualToString:sender.userInfo[@"name"]]) {
                _purposeStr = arr[indexD];
            }
            indexD ++;
        }
    }
    [ZPZNetRequest getMealsListtPageNum:@"1" pageSize:@"10" typeId:_typeStr ageId:_ageStr purposeId:_purposeStr supplierId:_supplierStr userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
            if (_dataSource.count > 0) {
                [_collectView reloadData];
                [_myLabel removeFromSuperview];
            }else{
                [self myLabel];
            }
        }else{
            [self myLabel];
        }
    }];

}

#pragma mark   ---  点击事件
- (void)classBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+115, MAX_Y(sender.frame)+305) DataSource:_classArr]];}

- (void)superBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+115, MAX_Y(sender.frame)+305) DataSource:_suppliersArr]];
}

- (void)peopleBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+115, MAX_Y(sender.frame)+305) DataSource:_incomeArr]];
}

- (void)typeBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+115, MAX_Y(sender.frame)+305) DataSource:_peopleArr]];
}

#pragma mark   ---  懒加载

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 80)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(0, self.bounds.size.height * 0.9);
        _scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshHeader];
        }];
    }
    return _scrollView;
}


- (UICollectionView *)collectView{
    if (!_collectView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat winth = (CGRectGetWidth(self.frame) - 60)/3;
        layout.itemSize = CGSizeMake(winth, winth);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 20;
        
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, CGRectGetWidth(self.frame), 340) collectionViewLayout:layout];
        _collectView.backgroundColor = [UIColor whiteColor];
        _collectView.showsHorizontalScrollIndicator = NO;
        _collectView.delegate = self;
        _collectView.contentInset = UIEdgeInsetsMake(-10, 10, 0, 0);
        _collectView.pagingEnabled = YES;
        _collectView.dataSource = self;
        _collectView.backgroundColor = [UIColor clearColor];
        [_collectView registerNib:[UINib nibWithNibName:@"MetalCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FundView"];
    }
    return _collectView;
}

- (UILabel *)myLabel{
    if (!_myLabel) {
        _myLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 140, 300, 200)];
        _myLabel.text = @"没有符合您搜索条件的数据";
        _myLabel.font = [UIFont systemFontOfSize:18 weight:2];
        _myLabel.textColor = WHITE_C;
        _myLabel.textAlignment = NSTextAlignmentCenter;
        _dataSource = [NSMutableArray array];
    }
    [_collectView reloadData];
    [self addSubview:_myLabel];

    return _myLabel;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MetalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FundView" forIndexPath:indexPath];
    NSDictionary *dic = _dataSource[indexPath.row];
    cell.titleLabel.text = dic[@"name"];
    [cell.bgImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",PHOTO_URL,dic[@"photo"]]]];
    return cell;
}



@end



